package cn.zyp.stusystem.controller;


import cn.zyp.stusystem.entity.Student;
import cn.zyp.stusystem.service.StudentService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    //获取学生列表
    @GetMapping("/list")
    public Map<String,Object> getStudentList(
            @RequestParam Integer page,  // 页码（从1开始）
            @RequestParam Integer size,  // 每页条数
            @RequestParam Integer grade,  // 年级（1-3）
            @RequestParam(required = false) Long classId,  // 班级ID
            @RequestParam(required = false) String name
    ){
        IPage<Student> studentPage = studentService.selectPageStudent(page,size,grade,classId,name);
        Map<String,Object> result = new HashMap<>();
        result.put("list",studentPage.getRecords());
        result.put("total",studentPage.getTotal());

        //测试
        System.out.println("后端返回的学生数据：" + result);

        return result;
    }

    //新增学生
    @PostMapping("/add")
    public boolean addStudent(@RequestBody Student student){
        return studentService.save(student);
    }

    //编辑学生
    @PutMapping("/edit")
    public boolean editStudent(@RequestBody Student student){
        return studentService.updateById(student);
    }

    //删除学生
    @DeleteMapping("/delete/{id}")
    public boolean deleteStudent(@PathVariable String id){
        if(id.contains(",")){
            String[] ids = id.split(",");
            return studentService.removeByIds(Arrays.asList(ids));
        }else {
            return studentService.removeById(id);
        }
    }
}
